//
//  CPColorMessageVC.m
//  CPApp
//
//  Created by 健健 on 2018/3/21.
//  Copyright © 2018年 Class. All rights reserved.
//

#import "CPColorMessageVC.h"
#import "CPHomeHeader.h"
#import "CPRightview.h"
#import "CPCombinationCell.h"
#import "CPColorMessageModel.h"
#import "CPHistoryLotteryCell.h"
#define api @"http://115.29.175.83/cpyc/"
@interface CPColorMessageVC ()
@property(nonatomic,strong)TZButtonsHeaderView * headerBts;
@property(nonatomic,strong) CPRightview * rightView;
@property(nonatomic,assign) BOOL needRight;
@property(nonatomic,strong) UIView *back;
@property(nonatomic,assign) BOOL isRightHide;

@property(nonatomic,strong) NSString * curindex;
@property(nonatomic,strong) NSString * lottype;
@property(nonatomic,strong) NSString * url;
@property(nonatomic,assign) NSInteger cellRow;
@property(nonatomic,strong) NSArray *ApiArr;
@end

@implementation CPColorMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title= @"彩讯";
    self.rightNavTitle = @"更多";
    self.curindex = @"0";
    self.cellRow = 0;
    self.lottype = @"1001";
    self.url = [api stringByAppendingString:@"gethitzhuhe.php"];
    [self configHeaderBtns];
    [self loadNetworkData];
    [mNotificationCenter addObserver:self selector:@selector(colorMsgNotification:) name:@"colorMsgNotification" object:nil];
    // 双色球
    self.ApiArr = @[@"gethitzhuhe.php",@"getlothis.php",@"getlottrend.php",@"zjnews.php",@"gcjqnews.php"];
}

-(void)configHeaderBtns {
    self.headerBts = [[TZButtonsHeaderView alloc]init];
    self.headerBts.frame = CGRectMake(0, 0, mScreen_W, 40);
    self.headerBts.shouldSelect = YES;
    self.headerBts.btnWidth = 100;
    self.headerBts.titles =@[@"大奖专家组合",@"历史开奖",@"走 势 图",@"中奖资讯",@"购彩技巧"];
    self.headerBts.selectBtnIndex = 0;
    self.headerBts.showSpots = @[@1,@1,@1,@1,@1];
    self.headerBts.changeFontWhenSelected = YES;
    MJWeakSelf
    [self.headerBts setDidClickButtonWithIndex:^(TZBaseButton *btn, NSInteger index) {
        if (index>1 &&index<5-2.5) {
            weakSelf.headerBts.contentOffset = CGPointMake(weakSelf.headerBts.btnWidth *(index -2) +weakSelf.headerBts.btnWidth/4, 0);
        }else if (index < 2) {
            weakSelf.headerBts.contentOffset = CGPointMake(0, 0);
        }else {
            weakSelf.headerBts.contentOffset = CGPointMake(weakSelf.headerBts.btnWidth *(5-2), 0);
        }
        weakSelf.cellRow = index;
        [weakSelf loadHeaderBtnWithIndex:index];
    }];
    [self.view addSubview:self.headerBts];
}
-(void)colorMsgNotification:(NSNotification *)not {
    self.needRight = NO;
    self.rightView.hidden = YES;
    self.back.hidden = YES;
    NSString * title = not.userInfo[@"title"];
    self.rightNavTitle = title;
    if ([title isEqualToString:@"双色球"]) {
        self.lottype = @"1001";
    }else if ([title isEqualToString:@"福彩3D"]) {
        self.lottype = @"1002";
    }else if ([title isEqualToString:@"七乐彩"]) {
        self.lottype = @"1003";
    }else if ([title isEqualToString:@"七星彩"]) {
        self.lottype = @"1004";
    }else if ([title isEqualToString:@"排列三"]) {
        self.lottype = @"1005";
    }else if ([title isEqualToString:@"排列五"]) {
        self.lottype = @"1006";
    }else {
        self.lottype = @"1007";
    }
    self.curindex = @"0";
    [self loadNetworkData];
}
-(void)loadHeaderBtnWithIndex:(NSInteger)index {
    NSArray * currentIndexS =@[@"0",@"2",@"3",@"4",@"5"];
    self.curindex = currentIndexS[index];
    self.url = [api stringByAppendingString:self.ApiArr[index]];
    [self loadNetworkData];
}
-(void)loadNetworkData {
    [ProgressHUD showLoading];
    [HttpTool getApi:self.url params:@{@"curindex":self.curindex,@"lottype":self.lottype} success:^(id responseData) {
        [ProgressHUD dismiss];
        NSArray * array = [CPColorMessageModel mj_objectArrayWithKeyValuesArray:responseData[@"list"]];
        [self.models removeAllObjects];
        [self.models addObjectsFromArray:array];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(NSString *errorMsg) {
        [ProgressHUD show:errorMsg];
    }];
}
-(void)configTableView {
    self.needHeaderRefresh = YES;
    self.tableViewStyle =UITableViewStylePlain;
    [super configTableView];
    self.tableView.frame = CGRectMake(0, 40, mScreen_W, mScreen_H-40-TabBar_H-Nav_H-10);
    [self.tableView registerCellByNibName:@"CPCombinationCell"];
    [self.tableView registerCellByNibName:@"CPHistoryLotteryCell"];
   
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CPHistoryLotteryCell * historyCell = [tableView dequeueReusableCellWithIdentifier:@"CPHistoryLotteryCell"];
    historyCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.cellRow==1) {
        CPColorMessageModel* model =self.models[indexPath.row];
        historyCell.lotteryModel = model;
        historyCell.labelView.hidden = NO;
        historyCell.contentLabel.hidden = YES;
        return historyCell;
    }else if (self.cellRow ==2) {
        UITableViewCell * trendCell = [tableView cellForRowAtIndexPath:indexPath];
        if (!trendCell) {
            trendCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        trendCell.selectionStyle = UITableViewCellSelectionStyleNone;
        trendCell.separatorInset = UIEdgeInsetsMake(0, 5, 0, 5);
        trendCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        CPColorMessageModel* trendModel =self.models[indexPath.row];
        trendCell.textLabel.text = trendModel.trendname;
        trendCell.textLabel.textColor = HJColor(240, 8, 7);
        trendCell.textLabel.font = HJFontSize(15);
        return trendCell;
    }else if (self.cellRow ==3) {
        CPColorMessageModel* pubaddrModel =self.models[indexPath.row];
        historyCell.pubaddrModel = pubaddrModel;
        historyCell.contentLabel.hidden = NO;
        historyCell.labelView.hidden = YES;
        return historyCell;
    }else if (self.cellRow ==4) {
        CPColorMessageModel* pubaddrModel =self.models[indexPath.row];
        historyCell.pubaddrModel = pubaddrModel;
        historyCell.contentLabel.hidden = NO;
        historyCell.labelView.hidden = YES;
        return historyCell;
    }else {
        CPCombinationCell * combinationCell = [tableView dequeueReusableCellWithIdentifier:@"CPCombinationCell"];
        CPColorMessageModel* model =self.models[indexPath.row];
        combinationCell.model =model;
        combinationCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return combinationCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellRow==1) {
        return 70;
    }else if (self.cellRow == 2) {
        return 45;
    }else if (self.cellRow == 4|| self.cellRow ==3) {
        return 60;
    }else {
       return 120;
    }
    return 0.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CPColorMessageModel* model =self.models[indexPath.row];
    TZWebViewController * webVC = [[TZWebViewController alloc]init];
    if (self.cellRow ==2) {
        webVC.navTitle = model.trendname;
        webVC.link_url = model.trendurl;
    }else if (self.cellRow==3 || self.cellRow ==4) {
        if (self.cellRow==3) {
            webVC.navTitle = @"中奖资讯";
        }else {
            webVC.navTitle = @"购彩技巧";
        }
        webVC.link_url = model.url;
    }else {
        return;
    }
    [self.navigationController pushViewController:webVC animated:YES];
}
-(void)didClickRightNavAction {
    self.isRightHide = !self.isRightHide;
    if (self.isRightHide) {
        self.needRight = YES;
        self.rightView.hidden = NO;
        self.back.hidden = NO;
        return;
    }
    self.needRight = NO;
    self.rightView.hidden = YES;
    self.back.hidden = YES;
}

-(void)setNeedRight:(BOOL)needRight {
    _needRight = needRight;
    if (needRight && !self.rightView && !self.back) {
        self.rightView = [[CPRightview alloc]init];
        self.rightView.frame = CGRectMake(0, 0, mScreen_W, 145);
        self.rightView.hidden = YES;
        self.rightView.navTitle = self.title;
        [self.view addSubview:self.rightView];
        [self.view bringSubviewToFront:self.rightView];
        
        self.back = [UIView new];
        self.back.backgroundColor = [UIColor blackColor];
        self.back.alpha = 0.3;
        self.back.hidden = YES;
        self.back.frame = CGRectMake(0, CGRectGetMaxY(self.rightView.frame), mScreen_W, mScreen_H);
        [self.view addSubview:self.back];
        [self.view bringSubviewToFront:self.back];
    }
}
@end
